package com;

import com.mapper.WorkerMapper;
import com.pojo.Worker;
import com.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class Workertest {
    @Test
    public void findWorkerByIdTest() {
        SqlSession session = MybatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        Worker worker = mapper.selectWorker(1);
        System.out.println(worker.toString());
        session.close();
    }
    @Test
    public void insertWorkerTest() {
        SqlSession session = MybatisUtils.getSession();
        Worker worker = new Worker();
        worker.setId(4);
        worker.setName("赵六");
        worker.setAge(36);
        worker.setSex("女");
        worker.setWord_id(1004);
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        int resule = mapper.insert(worker);
        if (resule > 0) {
            System.out.println("插入失败");
        }
        System.out.println(worker);
        session.commit();//增删改，一定要提交事务
        session.close();
    }
    @Test
    public void updateWorkerTest(){
        // 通过工具类生成SqlSession对象
        SqlSession session = MybatisUtils.getSession();
        Worker worker = new Worker();
        worker.setId(4);
        worker.setName("李华");
        worker.setAge(28);
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        // 更新员工信息
        int result = mapper.updateWorker(worker);
        if (result > 0){
            System.out.println("成功更新"+result+"条数据");
        }else {
            System.out.println("更新数据失败");
        }
        System.out.println(worker.toString());
        session.commit();
        // 关闭SqlSession
        session.commit();
    }
    @Test
    public void deleteWorkerTest(){
        // 通过工具类生成SqlSession对象
        SqlSession session = MybatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        // 删除员工信息
        int result = mapper.deleteWorker(4);
        if (result > 0){
            System.out.println("成功删除"+result+"条数据");
        }else {
            System.out.println("删除失败");
        }
        session.commit();
        // 关闭SqlSession
        session.close();
    }
    @Test
    public void selectWorkerByIdAndNameTest(){
        // 通过工具类生成SqlSession对象
        SqlSession session = MybatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        // 查询id为3,姓名为王五的员工信息
        Worker worker = mapper.selectWorkerByIdAndName(3,"王五");
        System.out.println(worker.toString());
        session.close();
    }
}
